package vn.fsoft.gst.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import vn.fsoft.gst.domain.Directorate;
import vn.fsoft.gst.domain.Organisation;
import vn.fsoft.gst.service.OrganisationService;

/**
 *
 *@category
 *implements DirectorateDAO
 * @author MinhNT
 *
 */
@Repository
public class DirectorateDAOImp implements DirectorateDAO {
    /**
     *@category autowired sessionFactory from Bean
    */
    @Autowired
    private SessionFactory sessionFactory;
    /**
     *@category autowired sessionFactory from Bean
    */
    @Autowired
    private OrganisationService organisationService;
    /**
     * @category add one directorate to database
     * @param directorate
     * input directorate
     * @return
     * output true or false
     */
    @Transactional
    public final  Boolean add(final Directorate directorate) {
        int flag = (Integer) sessionFactory.getCurrentSession().save(
                directorate);
        if (flag > 0) {
            return true;
        }
        return false;
    }
    /**
    *@category update one directorate to database
    * @param directorate
    * input directorate
    * @return
    * return true or false
    */
    @Transactional
    public final Boolean edit(final Directorate directorate) {
        sessionFactory.getCurrentSession().merge("Directorate",
        directorate);
        return true;
    }
    /**
    *@category get directorate by Id
    * @param id
    * input id of directorate
    * @return
    * return Directorate
    */
    @Transactional
    public final Directorate getDirectorateById(final int id) {
        Directorate directorate = (Directorate) sessionFactory
                .getCurrentSession().get(Directorate.class, id);
        return directorate;
    }
    /**
    *
    *@category get list directorate by Id from database
    * @param orgID
    * input orgID of Organisation
    * @return
    * return list all directorate by orgID
    */
    @SuppressWarnings("unchecked")
    @Transactional
    public final List<Directorate> getListDirectorateByOrgID(
                                   final int orgID) {
        Organisation organisation = organisationService
                .getOrganisationByID(orgID);
        Criteria criteria = sessionFactory.getCurrentSession()
                .createCriteria(
                Directorate.class);
        criteria.add(Restrictions.eq("organisation", organisation));
        return criteria.list();
    }
    /**
    *
    *@category active directorate by Id
    * @param directorateID
    * input1 directorateID of directorate
    * @param active
    * status of directorate
    * @return
    * return true or false
    */
    @Transactional
    public final Boolean setActiveDirectorate(final int directorateID,
                        final Boolean active) {
    try {
        Directorate directorate = (Directorate) sessionFactory.
                getCurrentSession().get(Directorate.class, directorateID);
        directorate.setIsActive(active);
        sessionFactory.getCurrentSession().merge("Directorate", directorate);
    } catch (HibernateException e) {
        return false;
    }
        return true;
    }
}
